package com.joshua.ticket.manager.controllers;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joshua.userservice.models.Continent;
import com.joshua.userservice.models.Product;

@Controller
@RequestMapping("/product")
public class ProductController {

	@Autowired
	public SessionFactory sessionFactory;

	@RequestMapping(value = "getall", method = RequestMethod.GET)
	public @ResponseBody
	List<Product> getAll() {
		List<Product> products = new ArrayList<Product>();

		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();

			products = (List<Product>) session.createCriteria(Product.class)
					.add(Restrictions.eq("catagory", "hat")).list();
			/*
			 * for (Object o : list) { Product p = (Product) o;
			 * System.out.print(p.getCode()); products.add(p); }
			 */

			tx.commit();
			session.close();

		} catch (Exception ex) {
			System.out.print(ex.getMessage());
		}

		return products;
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public @ResponseBody
	JsonResponse create(@RequestBody Product product) {
		JsonResponse jsonResponse = new JsonResponse();
		// String id = "999";
		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.save(product);
			tx.commit();
			session.close();

			jsonResponse.setException("None");
			jsonResponse.setResultState("Success");
			jsonResponse.setResult("Create Continent with id:"
					+ product.getId());
		} catch (Exception ex) {
			jsonResponse.setException(ex.toString());
			jsonResponse.setResultState("Failure");
			jsonResponse.setResult("Create Continent with id:"
					+ product.getCode() + "Failure");
		}

		return jsonResponse;
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public @ResponseBody
	JsonResponse update(@RequestBody Product product) {
		JsonResponse jsonResponse = new JsonResponse();
		// String id = "999";
		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.merge(product);
			tx.commit();
			session.close();

			jsonResponse.setException("None");
			jsonResponse.setResultState("Success");
			jsonResponse.setResult("Update Continent with id:"
					+ product.getId());
		} catch (Exception ex) {
			jsonResponse.setException(ex.toString());
			jsonResponse.setResultState("Failure");
			jsonResponse.setResult("Update Continent with id:"
					+ product.getCode() + "Failure");
		}

		return jsonResponse;
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public @ResponseBody
	JsonResponse delete(@RequestBody Product product) {
		JsonResponse jsonResponse = new JsonResponse();
		// String id = "999";
		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.delete(product);
			tx.commit();
			session.close();

			jsonResponse.setException("None");
			jsonResponse.setResultState("Success");
			jsonResponse.setResult("Delete Continent with id:"
					+ product.getId());
		} catch (Exception ex) {
			jsonResponse.setException(ex.toString());
			jsonResponse.setResultState("Failure");
			jsonResponse.setResult("Delete Continent with id:"
					+ product.getCode() + "Failure");
		}

		return jsonResponse;
	}
	
	
}
